package com.example.haojiutao.controller.user;

import com.example.haojiutao.common.Result;
import com.example.haojiutao.model.domain.Collect;
import com.example.haojiutao.service.CollectService;
import com.example.haojiutao.service.CommentService;
import com.example.haojiutao.service.GoodsService;
import com.example.haojiutao.service.UserService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 收藏控制层
 * @Author: ma_lihong
 * @CreateTime: 2024/1/31 16:53
 */
@Slf4j
@RestController
public class CollectController {
    @Autowired
    private CollectService collectService;

//    @Autowired
//    private GoodsService goodsService;
//    @Autowired
//    private CommentService commentService;
//    @Autowired
//    private UserService userService;
/*
 * @Description: 收藏商品
 * @Author: ma_lihong
 * @CreateTime: 2024/1/31 17:00
 * @param: collect
 * @return: com.example.haojiutao.common.Result
 **/
    @PostMapping("collect/add")
    public Result addCollect(@RequestBody Collect collect){
        log.info("收藏：{}",collect);
        collect.setCollectTime(new Date());
        Collect collectinfo=collectService.findCollect(collect.getUserId(),collect.getGoodsId());
        if(null ==collectinfo){
            collect.setStatus(1);
            collectService.addCollect(collect); //收藏

        }else {
            collectService.updateCollectStatus(collectinfo.getId());

        }
        return Result.success("收藏成功");


    }

    /*
     * @Description: 根据,goodsId查看收藏该商品的信息
     * @Author: ma_lihong
     * @CreateTime: 2024/2/1 16:27
     * @param: goodsId
     * @return: com.example.haojiutao.common.Result
     **/
@GetMapping("collect/{goodsId}")
    public Result getByGoodsId(@PathVariable int goodsId){
    List<Collect> list =collectService.getByGoodsId(goodsId);
        return Result.success("查看成功",list);

    }


    /*
     * @Description: 根据,userid查看收藏该商品
     * @Author: ma_lihong
     * @CreateTime: 2024/2/1 16:27
     * @param: userId
     * @return: com.example.haojiutao.common.Result
     **/
    @GetMapping("/collectbyUserId/{userId}")
    public Result getByUserId(@PathVariable int userId){
        //List<Collect> collectList = new ArrayList<>();
        Collect collect=new Collect();
        collectService.findByUserId(userId);

            //获取收藏数
            if(collect !=null){
                collectService.getCountByGoodsId(collect.getGoodsId());
            }
        PageInfo pageInfo=new PageInfo<>();
        return Result.success("查看成功",pageInfo);

    }
    /*
     * @Description: 查询用户所有收藏
     * @Author: ma_lihong
     * @CreateTime: 2024/2/2 16:29
     * @param: collect
     * @return: com.example.haojiutao.common.Result
     **/
    @GetMapping("/collect/list")
    public Result selectbylist(Collect collect){
        log.info("列表查询参数 ：{}" ,collect);
        List<Collect> collects =collectService.selectCollect(collect);
        PageInfo<Collect> pageInfo=new PageInfo<>(collects);
        return Result.success("查询成功",collects);
    }

    /*
     * @Description: 根据goods_id 删除收藏记录
     * @Author: ma_lihong
     * @CreateTime: 2024/2/2 16:29
     * @param: id
     * @return: int
     **/
//    @DeleteMapping("collect/delete")
//    public int deleteCollect(int id){
//        return collectService.deleteCollect(id);
//    }
    @DeleteMapping("collect/{id}")
    public int deleteCollect1(@PathVariable("id") int id){

        return collectService.deleteCollect(id);
    }


    
}
